package app.server.project.main.service.db.dao.cfg;

import app.server.project.main.service.db.entity.cfg.LangCfgEntity;
import app.server.project.main.service.db.mapper.cfg.TaskLangMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2025-01-16 15:25:22
 */
@Repository
@Data
public class LangCfgDao extends ServiceImpl<TaskLangMapper, LangCfgEntity> {


    private Map<Long, List<LangCfgEntity>> typeMap = new HashMap<>();


    public void flush() {
        var list = this.list();
        this.typeMap = list.stream().collect(Collectors.groupingBy(LangCfgEntity::getLid));
    }

    @PostConstruct
    public void init() {
        this.flush();
    }

    public LangCfgEntity getLangId(long lid, String type) {
        var list = this.typeMap.get(lid);
        if (list == null) return null;
        var one = list.stream().filter(v -> type.equals(v.getType())).findAny();
        if (one.isEmpty()) return null;
        return one.get();
    }


    public void del(String langId) {
        this.removeById(langId);
    }
}




